package com.gongpingjialoan.activity.vip;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.activity.main.LoginActivity;
import com.gongpingjialoan.bean.AppData;
import com.gongpingjialoan.bean.Monitor;
import com.gongpingjialoan.bean.VIPEb;
import com.gongpingjialoan.data.UserManager;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.utility.StepMonitor;
import com.gongpingjialoan.wxapi.WXPayEntryActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/5.
 */
public class VIPDetailActivity extends BaseActivity {

    final int LOGIN = 1001;
    UserManager mUserManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vip_detail_activity);
        EventBus.getDefault().register(this);
        initView();
    }

    private void initView() {

        String from = getIntent().getStringExtra("from");
        if (!TextUtils.isEmpty(from)) {
            Map<String, Object> map = new HashMap<>();
            map.put("from", from);
            StepMonitor.getInstance().addMonitor(new Monitor("enter", "vipIntroduction", System.currentTimeMillis(), 1f, map));
        }

        setTitle("公平价VIP");
        findViewById(R.id.buy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (GPJApplication.getInstance().isLogin()) {
                    Intent it = new Intent(VIPDetailActivity.this, WXPayEntryActivity.class);
                    it.putExtra("action_type", "vip_buy");
                    startActivity(it);
                } else {
                    Intent it = new Intent(VIPDetailActivity.this, LoginActivity.class);
                    startActivityForResult(it, LOGIN);
                }
                StepMonitor.getInstance().addMonitor(new Monitor("click", "vipIntroductionPay", System.currentTimeMillis(), 1f));

            }
        });
        findViewById(R.id.info).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(VIPDetailActivity.this, VIPInfoActivity.class);
                startActivity(it);


            }
        });


        TextView priceT = (TextView) findViewById(R.id.price);

        AppData data = GPJApplication.getInstance().mAppData;
        if (data != null) {
            priceT.setText("¥" + (int)(data.getData().getVip_charge_price().getValue() / 100f));
        }
        mUserManager = new UserManager(VIPDetailActivity.this);
        mUserManager = mUserManager.LoadUserInfo();
        findViewById(R.id.buy_layout).setVisibility(mUserManager.is_vip() ? View.GONE : View.VISIBLE);

        TextView old_priceT = (TextView) findViewById(R.id.old_price);
        old_priceT.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {

                case LOGIN:
                    mUserManager = mUserManager.LoadUserInfo();
                    if (mUserManager.is_vip()) {
                        findViewById(R.id.buy_layout).setVisibility(View.GONE);
                    } else {
                        Intent it = new Intent(VIPDetailActivity.this, WXPayEntryActivity.class);
                        it.putExtra("action_type", "vip_buy");
                        startActivity(it);
                    }
                    break;

                default:
                    break;
            }

        }
    }


    @Subscribe
    public void onEventMainThread(VIPEb VIPEb) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
